package com.augurit.common.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.OmUserOrg;
import com.augurit.common.system.convert.OmUserOrgConverter;
import com.augurit.common.system.web.org.form.OmUserOrgForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.CollectionUtils;

/**
 * 用户机构关联DAO类
 */
@Repository
public class OmUserOrgDao extends BaseDao<OmUserOrg, Long> {

	/**
	 * 保存用户机构关联Form对象
	 * @param form 用户机构关联Form对象
	 */
	public void save(OmUserOrgForm form){
		
		if(form != null){
			OmUserOrg entity = null;
			
			//准备VO对象
			if(form != null && form.getUoId() != null){
				entity = this.get(form.getUoId());
			}else{
				entity = new OmUserOrg();
			}
			
			//属性值转换
			OmUserOrgConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setUoId(entity.getUoId());
		}
	}

	/**
	 * 根据用户ID查询关联机构ID列表
	 * @param userId 用户ID
	 * @return 关联机构ID列表
	 */
	public List<Long> getOrgIds(Long userId){
		return partSelectOmUserOrg(userId, "orgId");
	}
	
	/**
	 * 根据用户ID查询关联OmUserOrg对象列表
	 * @param userId 用户ID
	 * @return OmUserOrg对象列表
	 */
	public List<OmUserOrg> getByUserId(Long userId){
		return partSelectOmUserOrg(userId, null);
	}
	
	private List partSelectOmUserOrg(Long userId, String entityProperty){
		String hql = "select " 
						+ (entityProperty != null && entityProperty.trim().length() > 0 ? "ps." + entityProperty : "ps") 
						+ " from OmUserOrg ps where ps.userId = ?";
		List values = new ArrayList();
		values.add(userId);
		
		return this.find(hql,values);
	}
	
	/**
	 * 根据用户ID删除对象
	 * @param userId 用户ID
	 */
	public void deleteByUserId(Long userId){
		if(userId != null){
			List<OmUserOrg> entities = getByUserId(userId);
			if(entities != null && entities.size() > 0)
				this.delete(entities);
		}
	}
	
	/**
	 * 根据机构ID查询关联OmUserOrg对象列表
	 * @param orgId 机构ID
	 * @return 关联OmUserOrg对象列表
	 */
	public List<OmUserOrg> getByOrgId(Long orgId){
		if(orgId != null){
			String hql = "from OmUserOrg ps where ps.orgId=?";
			List values = new ArrayList();
			values.add(orgId);
			return this.find(hql, values);
		}else
			return null;
	}
	
	/**
	 * 根据机构id数组获取其下的所有的用户
	 * @param orgIds 机构id数组
	 * @return 用户id数组
	 * @author 王剑文
	 */
	public Long[] getUserIdsByOrgIds(Long[] orgIds){
		
		if( orgIds != null && orgIds.length > 0){
			Set<Long> userIdSet = new HashSet();
			String hql = "select distinct ps.userId from OmUserOrg ps where ";
			String condition = HqlUtils.buildOrCondition("ps", "orgId", orgIds);
			hql += condition;
			List<Long> userIdList = this.find(hql);
			
			userIdSet.addAll(userIdList);
			return CollectionUtils.toLongArray(userIdSet);
		}
		return null;
	}
	
	/**
	 * 根据机构ID删除对象
	 * @param orgId 机构ID
	 */
	public void deleteByOrgId(Long orgId){
		List<OmUserOrg> list = getByOrgId(orgId);
		if(list != null && list.size() > 0){
			this.delete(list);
		}
	}
	
	/**
	 * 根据用户ID和机构ID查找OmUserOrg对象
	 * @param userId 用户ID
	 * @param orgId 机构ID
	 * @return
	 */
	public OmUserOrg get(Long userId, Long orgId){
		if(userId != null && orgId != null){
			String hql = "from OmUserOrg ps where ps.userId=? and ps.orgId=?";
			List values = new ArrayList();
			values.add(userId);
			values.add(orgId);
			return this.findUnique(hql, values);
		}else
			return null;
	}
	
	/**
	 * 根据机构id和用户id保存一个OmUserOrgForm对象
	 * @param userId 用户ID
	 * @param orgId 机构ID
	 */
	public void save(Long userId, Long orgId){
		if(userId != null && orgId != null){
			OmUserOrg entity = new OmUserOrg();
			entity.setOrgId(orgId);
			entity.setUserId(userId);
			entity.setIsMain(OrgConstant.ZERO);
			this.save(entity);
		}
	}

	/**
	 * 移动用户到另一机构
	 * @param  userId 用户主键
	 * @param targetOrgId 要移动到的机构主键
	 * */
	public void moveUserToOtherOrg(Long userId, Long oldOrgId,Long targetOrgId){
		if(userId != null && oldOrgId != null &&targetOrgId != null){
			
			//删除userId和oldOrgId对应的OmUserOrgForm对象
			OmUserOrgForm form = OmUserOrgConverter.convertToForm(get(userId, oldOrgId));
			if(form != null)
				this.delete(form.getUoId());
			
			//增加userId和targetOrgId对应的OmUserOrgForm对象
			this.save(userId, targetOrgId);
		}
	}
	
	/**
	 * 设置主机构
	 * @param userId 用户ID
	 * @param orgId 机构ID
	 */
	public void setMainOrg(Long userId, Long orgId){
		if(userId != null && orgId != null){
			//获取指定用户关联OmUserOrg对象列表
			List<OmUserOrg> list = this.getByUserId(userId);
			
			if(list != null && list.size() > 0){
				for(OmUserOrg entity : list){
					
					if(entity.getOrgId().longValue() == orgId.longValue())
						entity.setIsMain(OrgConstant.ONE);
					else
						entity.setIsMain(OrgConstant.ZERO);
					
					this.save(entity);
				}
			}
		}
	}
	
	/**
	 * 删除用户与机构的关联，不删除用户本身
	 * @param userId 用户主键
	 * @param orgId  机构主键
	 */
	public void deleteUserFromOrg(Long userId, Long orgId){
		if(userId != null && orgId != null){
			String hql = "from OmUserOrg ps where userId = ? and orgId = ?";
			List<Long> values = new ArrayList<Long>();
			values.add(userId);
			values.add(orgId);
			
			OmUserOrg entity = (OmUserOrg)this.findUnique(hql, values);
			this.delete(entity);
		}
	}
}
